# -*- coding:utf-8 -*-

from scrapy import Request
from scrapy.spiders import Spider
from scrapyExercise.items import DoubanMoive
import json

class DbMovieSpider(Spider):
    name = "dbmovie"
    headers = {"User_Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"}
    currentPage = 1

    def start_requests(self):
        url = "https://movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=%E7%94%B5%E5%BD%B1&start=0&countries=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86"
        yield Request(url, headers=self.headers)

    def parse(self, response, **kwargs):
        item = DoubanMoive()
        json_text = response.text
        movie_dict = json.loads(json_text).get('data',[])
        for one_movie in movie_dict:
            item['title'] = one_movie['title']
            item['directors'] = one_movie['directors']
            item['casts'] = one_movie['casts']
            item['rate'] = one_movie['rate']

            yield item

        if len(movie_dict) != 0:
            next_url = "https://movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=%E7%94%B5%E5%BD%B1&start={0}&countries=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86".format(self.currentPage*20)
            self.currentPage += 1
            yield Request(next_url, headers=self.headers)